﻿using mozhi.smarterp.Business.Products.Repositories;
using Mozhi.Abp.Domain.BasicData;
using Mozhi.Abp.Domain.BasicData.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using Volo.Abp.Guids;

namespace mozhi.smarterp.Business.Products.Services
{
    public class ProductPurchaseSettingManager : DomainService
    {
        private IProductRepository _productRepository;
        private IUnitRepository _unitRepository;
        private ISupplierRepository _supplierRepository;
        private IEmployeeRepository _employeeRepository;
        private IGuidGenerator _guidGenerator;
        private IProductAuxiliaryUnitRepository _auxiliaryUnitRepository;
        private IProductPurchaseSettingRepository _repository;

        public ProductPurchaseSettingManager(IProductRepository productRepository, IUnitRepository unitRepository, ISupplierRepository supplierRepository, IEmployeeRepository employeeRepository, IGuidGenerator guidGenerator, 
            IProductAuxiliaryUnitRepository auxiliaryUnitRepository , IProductPurchaseSettingRepository repository)
        {
            _productRepository = productRepository;
            _unitRepository = unitRepository;
            _supplierRepository = supplierRepository;
            _employeeRepository = employeeRepository;
            _guidGenerator = guidGenerator;
            _auxiliaryUnitRepository = auxiliaryUnitRepository;
            _repository = repository;
        }

        public async Task<ProductPurchaseSetting> GetAsync(Guid? id , Guid productId, Guid purchaseUnitId , Guid defaultSupplierId , Guid defaultPurchaserId , decimal? minPurchaseQuantity)
        {
            var matchCount = await _repository.GetCountAsync(productId , id);

            if (matchCount > 0)
                throw new UserFriendlyException($"产品：{productId}采购信息已存在", "100000");

            var product = await _productRepository.GetAsync(productId);

            var purchaseUnit = await _unitRepository.GetAsync(purchaseUnitId);

            var auxiliaryUnit = product.BasicUnitId != purchaseUnitId ? await _auxiliaryUnitRepository.GetAsync(productId, purchaseUnitId) : null;

            var supplier = await _supplierRepository.GetAsync(defaultSupplierId);

            var purchaser = await _employeeRepository.GetAsync(defaultPurchaserId);

            ProductPurchaseSetting purchaseSetting;

            if (id.HasValue)
            {
                purchaseSetting = await _repository.GetAsync(id.Value);

                if (purchaseSetting == null)
                    throw new UserFriendlyException($"产品采购设置：{id.Value}不存在");

                purchaseSetting.Change(product, purchaseUnit, auxiliaryUnit, supplier, purchaser, minPurchaseQuantity);
            }
            else
                purchaseSetting = new ProductPurchaseSetting(_guidGenerator.Create(), product, purchaseUnit, auxiliaryUnit, supplier, purchaser, minPurchaseQuantity);

            return purchaseSetting;
        }
    }
}
